package cn.iocoder.yudao.module.wecom.controller.admin.dept;

import cn.iocoder.yudao.module.wecom.convert.dept.DeptConverter;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wecom.controller.admin.dept.vo.*;
import cn.iocoder.yudao.module.wecom.dal.dataobject.dept.WeDeptDO;
import cn.iocoder.yudao.module.wecom.service.dept.WeDeptService;

@Tag(name = "管理后台 - 企微部门")
@RestController
@RequestMapping("/wecom/we-dept")
@Validated
public class WeDeptController {

    @Resource
    private WeDeptService weDeptService;

    @PostMapping("/create")
    @Operation(summary = "创建企微部门")
    @PreAuthorize("@ss.hasPermission('wecom:we-dept:create')")
    public CommonResult<Long> createWeDept(@Valid @RequestBody WeDeptSaveReqVO createReqVO) {
        return success(weDeptService.createWeDept(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新企微部门")
    @PreAuthorize("@ss.hasPermission('wecom:we-dept:update')")
    public CommonResult<Boolean> updateWeDept(@Valid @RequestBody WeDeptSaveReqVO updateReqVO) {
        weDeptService.updateWeDept(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除企微部门")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wecom:we-dept:delete')")
    public CommonResult<Boolean> deleteWeDept(@RequestParam("id") Long id) {
        weDeptService.deleteWeDept(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得企微部门")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wecom:we-dept:query')")
    public CommonResult<WeDeptRespVO> getWeDept(@RequestParam("id") Long id) {
        WeDeptDO weDept = weDeptService.getWeDept(id);
        return success(BeanUtils.toBean(weDept, WeDeptRespVO.class));
    }
//
//    @GetMapping("/page")
//    @Operation(summary = "获得企微部门分页")
//    @PreAuthorize("@ss.hasPermission('wecom:we-dept:query')")
//    public CommonResult<PageResult<WeDeptRespVO>> getWeDeptPage(@Valid WeDeptPageReqVO pageReqVO) {
//        PageResult<WeDeptDO> pageResult = weDeptService.getWeDeptPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, WeDeptRespVO.class));
//    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得企微部门列表")
    public CommonResult<List<WeDeptRespVO>> getWeDeptList(@Valid WeDeptPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeDeptDO> pageResult = weDeptService.getWeDeptPage(pageReqVO).getList();
        return success(DeptConverter.INSTANCE.convert1(pageResult));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出企微部门 Excel")
    @PreAuthorize("@ss.hasPermission('wecom:we-dept:export')")
    @OperateLog(type = EXPORT)
    public void exportWeDeptExcel(@Valid WeDeptPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeDeptDO> list = weDeptService.getWeDeptPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "企微部门.xls", "数据", WeDeptRespVO.class,
                        BeanUtils.toBean(list, WeDeptRespVO.class));
    }

}